﻿namespace Microsoft.VisualStudio.ServiceModel.DomainServices.Tools
{
    using EnvDTE;
    using EnvDTE80;
    using Microsoft.VisualStudio.TemplateWizard;
    using Microsoft.Win32;
    using System;
    using System.Collections.Generic;
    using System.Globalization;

    public class BusinessApplicationServerTemplateWizard : IWizard
    {
        private DTE2 _dte2;

        public void BeforeOpeningFile(ProjectItem projectItem)
        {
        }

        private static string GetSilverlightRuntimeVersion()
        {
            string silverlightToolsVersion = TemplateUtilities.GetSilverlightToolsVersion();
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(string.Format(CultureInfo.InvariantCulture, @"SOFTWARE\Microsoft\Microsoft SDKs\Silverlight\{0}\ReferenceAssemblies", new object[] { silverlightToolsVersion })))
            {
                if (key != null)
                {
                    string str2 = key.GetValue("SLRuntimeInstallVersion") as string;
                    if (!string.IsNullOrEmpty(str2))
                    {
                        return str2;
                    }
                }
            }
            return null;
        }

        public void ProjectFinishedGenerating(Project project)
        {
        }

        public void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {
        }

        public void RunFinished()
        {
        }

        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            this._dte2 = (DTE2) automationObject;
            replacementsDictionary["$safeclientprojectname$"] = (string) this._dte2.Globals["safeclientprojectname"];
            replacementsDictionary["__safeclientprojectname__"] = replacementsDictionary["$safeclientprojectname$"];
            replacementsDictionary["$runtime_version$"] = GetSilverlightRuntimeVersion() ?? string.Format(CultureInfo.InvariantCulture, "{0:0.0}", new object[] { 4.0M });
        }

        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }
    }
}

